//
// Created by benxb on 2021/11/9.
//

#include <string>
#include <gtest/gtest.h>
#include <compress/compressor.h>
#include <fstream>
#include "common/logger.h"
#include "mem/active_data_page.h"

using namespace benxdb;

const std::string data_file = "/home/ubuntu/benxdb/test/compress/lidar_data_0.txt";
const std::string out_file = "/home/ubuntu/var/lidar_data_0.txt";

void TestAppendUtil(Value val) {
    static ActiveDataPage page(TypeId::DECIMAL);
    bool succ = page.Append(val);
    if (!succ) {
        LOG_DEBUG("clear page.");
        page.Clear();
        page.Append(val);
    }
}

// Test write time when adapt row layout
TEST(MemLayoutWriteTest, TestRowLayout) {
    benxdb::Compressor compressor;
    std::ifstream data_io(data_file, std::ios::binary | std::ios::in);
    if (!data_io) {
        LOG_ERROR("file read error");
        return;
    }
    char line[100];
    double x, y, z, intensity;
    while (data_io.getline(line, 100)) {
        sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &intensity);
        TestAppendUtil(Value(TypeId::DECIMAL, x));
        TestAppendUtil(Value(TypeId::DECIMAL, y));
        TestAppendUtil(Value(TypeId::DECIMAL, z));
        TestAppendUtil(Value(TypeId::DECIMAL, intensity));
    }
}

void TestAppendUtil(ActiveDataPage& page, Value val) {
    bool succ = page.Append(val);
    if (!succ) {
        LOG_DEBUG("clear page.");
        page.Clear();
        page.Append(val);
    }
}

// Test write time when adapt column layout
TEST(MemLayoutWriteTest, TestColumnLayout) {
    benxdb::Compressor compressor;
    std::ifstream data_io(data_file, std::ios::binary | std::ios::in);
    if (!data_io) {
        LOG_ERROR("file read error");
        return;
    }
    char line[100];
    double x, y, z, intensity;
    ActiveDataPage xp(TypeId::DECIMAL), yp(TypeId::DECIMAL), zp(TypeId::DECIMAL), inp(TypeId::DECIMAL);
    while (data_io.getline(line, 100)) {
        sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &intensity);
        TestAppendUtil(xp, Value(TypeId::DECIMAL, x));
        TestAppendUtil(yp, Value(TypeId::DECIMAL, y));
        TestAppendUtil(zp, Value(TypeId::DECIMAL, z));
        TestAppendUtil(inp, Value(TypeId::DECIMAL, intensity));
    }
}

// Test the time when write into file directly
TEST(MemLayoutWriteTest, TestWriteFile) {
    benxdb::Compressor compressor;
    std::ifstream data_io(data_file, std::ios::binary | std::ios::in);
    if (!data_io) {
        LOG_ERROR("file read error");
        return;
    }
    char line[100];
    double x, y, z, intensity;
    std::ofstream ofs(out_file, std::ios::out | std::ios::binary);
    while (data_io.getline(line, 100)) {
        sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &intensity);
        // ofs << x << y << z << intensity << std::endl;
        ofs.write((char*)&x, sizeof(x));
        ofs.write((char*)&y, sizeof(y));
        ofs.write((char*)&z, sizeof(z));
        ofs.write((char*)&intensity, sizeof(intensity));
        ofs.flush();
    }
    ofs.close();
}